package com.jetwinner.webfast.mvc.controller.admin;

import com.jetwinner.toolbag.ArrayToolkit;
import com.jetwinner.util.EasyStringUtil;
import com.jetwinner.webfast.kernel.Paginator;
import com.jetwinner.webfast.kernel.dao.support.OrderBy;
import com.jetwinner.webfast.kernel.service.AppSettingService;
import com.jetwinner.webfast.kernel.service.AppUserService;
import com.jetwinner.webfast.kernel.typedef.ParamMap;
import com.jetwinner.webfast.module.bigapp.service.AppGroupService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author jingjianxin, xulixin
 */
@Controller("adminGroupController")
public class GroupController {

    private final AppSettingService settingService;
    private final AppGroupService groupService;
    private final AppUserService userService;

    public GroupController(AppSettingService settingService,
                           AppGroupService groupService,
                           AppUserService userService) {

        this.settingService = settingService;
        this.groupService = groupService;
        this.userService = userService;
    }

    @RequestMapping("/admin/group")
    public String indexPage(HttpServletRequest request, Model model) {
        Map<String, Object> fields = ParamMap.toConditionMap(request);
        Map<String, Object> conditions = new ParamMap()
                .add("status", null)
                .add("title", null)
                .add("ownerName", null).toMap();

        if (!CollectionUtils.isEmpty(fields)) {
            conditions.keySet().forEach(key -> {
                if (EasyStringUtil.isNotBlank(fields.get(key))) {
                    conditions.put(key, fields.get(key));
                }
            });
        }

        Paginator paginator = new Paginator(request, this.groupService.searchGroupsCount(conditions), 10);

        List<Map<String, Object>> groupinfo = groupService.searchGroups(conditions,
                OrderBy.build(1).addDesc("createdTime"),
                paginator.getOffsetCount(), paginator.getPerPageCount());

        Set<Object> ownerIds =  ArrayToolkit.column(groupinfo, "ownerId");
        model.addAttribute("owners", userService.findUsersByIds(ownerIds));
        model.addAttribute("groupinfo", groupinfo);
        model.addAttribute("paginator", paginator);
        return "/admin/group/index";
    }

    @RequestMapping("/admin/group/set")
    public String groupSetPage(HttpServletRequest request) {
        if ("POST".equalsIgnoreCase(request.getMethod())) {
            Map<String, Object> set = ParamMap.toFormDataMap(request);

            this.settingService.set("group", set);
        }

        return "/admin/group/set";
    }

    @RequestMapping("/admin/group/thread")
    public String threadPage() {
        return "/admin/group/thread";
    }
}
